﻿using NUnit.Framework;
using StoryQ;
using TripleA.Core.Interfaces.Exceptions;
using TripleA.StoryQ;

namespace TripleA.Tests.Registry
{
    [TestFixture]
    public class RegistryTests : BddFeature
    {
        protected override Feature DescribeFeature()
        {
            return new Story("Testing the registry extensions are sweeeet")
                .InOrderTo("interact with the registry")
                .AsA("system test")
                .IWant("the registry extensions to handle this for me")
                .Tag("Registry");
        }

        [Test]
        [TestCase(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\AboutURLs\blank", "res://mshtml.dll/blank.htm")]
        [TestCase(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Desktop Search\Version", "6.1.7601.17610")]
        public void RegistryKeyValueIsCorrect(string path, string value)
        {
            using (var domain = new RegistryDomain())
            {
                Feature.WithScenario("key '{0}' should have value '{1}'", path, value)
                    .Given(domain.TheRegistryKey_, path)
                        .And(domain.TheExpectedKeyValue_, value)
                    .When(domain.TheRegistryKeyValueIsChecked)
                    .Then(domain.ThrewNoException)
                    .ExecuteWithReport();
            }
        }

        [Test]
        public void InvalidRegistryPathThrowsCorrectException()
        {
            using (var domain = new RegistryDomain())
            {
                Feature.WithScenario("key does not exist so expect the correct exception to be thrown")
                    .Given(domain.TheRegistryKey_, @"HKEY_LOCAL_MACHINE\SOFTWARE\34896n1238947")
                        .And(domain.TheExpectedKeyValue_, string.Empty)
                    .When(domain.TheRegistryKeyValueIsChecked)
                    .Then(domain.TheTypeForException_Is_, 0, typeof(ExpectationException))
                        .And(domain.TheMessageForException_Contains_, 0, "not exist")
                    .ExecuteWithReport();
            }
        }

        [Test]
        public void IncorrectRegistryValueThrowsCorrectException()
        {
            using (var domain = new RegistryDomain())
            {
                Feature.WithScenario("incorrect key value throws correct exception")
                    .Given(domain.TheRegistryKey_, @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\AboutURLs\blank")
                        .And(domain.TheExpectedKeyValue_, "bad-value")
                    .When(domain.TheRegistryKeyValueIsChecked)
                    .Then(domain.TheTypeForException_Is_, 0, typeof(ExpectationException))
                        .And(domain.TheMessageForException_Contains_, 0, "expected 'bad-value'")
                    .ExecuteWithReport();
            }
        }
    }
}